//
//  UpdateTaskLogViewController.m
//  OAProject
//
//  Created by yhj on 2017/3/29.
//  Copyright © 2017年 cdnunion. All rights reserved.
//  更新日志主界面

#import "UpdateTaskLogViewController.h"
#import "TaskListViewController.h"

@interface UpdateTaskLogViewController ()<ASProgressPopUpViewDataSource,UITextViewDelegate>

@property(nonatomic,strong)UIButton *stopBtn;

@property(nonatomic,strong)UIButton *endBtn;

@property(nonatomic,strong)ASProgressPopUpView *progressPopUpView;

@property(nonatomic,assign)BOOL isEndProgress;

@property(nonatomic,assign)BOOL isEndBtn;

@end

@implementation UpdateTaskLogViewController

-(void)progressViewWillDisplayPopUpView:(ASProgressPopUpView *)progressView
{


}

- (NSArray *)allStringsForProgressView:(ASProgressPopUpView *)progressView
{
    NSArray *array;
    return array;
}


-(NSString *)progressView:(ASProgressPopUpView *)progressView stringForProgress:(float)progress
{
    NSString *s;
    return s;
}


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title=@"更新日志";

    _isEndProgress=NO;

    _isEndBtn=NO;

    self.view.backgroundColor=WhiteColor;

    // 进度
    _progressPopUpView=[[ASProgressPopUpView alloc]initWithFrame:CGRectMake(kmargin*2,64+60,APPW-kmargin*4,1)];
    if (self.isStop==YES)
    {
      _progressPopUpView.popUpViewAnimatedColors=@[[UIColor redColor],[UIColor redColor]];
    }
    else
    {
      _progressPopUpView.popUpViewAnimatedColors=@[MainColor,MainColor];
    }
    _progressPopUpView.popUpViewCornerRadius=kmargin/5;
    _progressPopUpView.font=Font_Number(24);
    _progressPopUpView.progress=[self.tempo floatValue]/100;
    [_progressPopUpView showPopUpViewAnimated:YES];
    _progressPopUpView.backgroundColor=LightGreyColor;
    [self.view addSubview:_progressPopUpView];


    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0,64+80,APPW,60)];
    view.backgroundColor=BGColor;
    [self.view addSubview:view];

    // 终止
    self.endBtn=[AppUI createButtonWithSupeView:self.view andWithNormalImage:[UIImage imageNamed:@"endIcon"] andWithSelectImage:[UIImage imageNamed:@"endIcon"] andWithBtnAction:^{

        UIAlertView *alert=[UIAlertView alertViewWithTitle:@"提示" message:@"你确定要终止当前进度吗？" cancelButtonTitle:@"取消" didDismissBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {

            if (buttonIndex==0)
            {
                self.progressPopUpView.popUpViewAnimatedColors=@[[UIColor redColor],[UIColor redColor]];
                self.stopBtn.enabled=NO;
                self.endBtn.enabled=NO;
                _isEndProgress=YES;
            }
        } otherButtonTitles:@"确定", nil];
        [alert show];
    }];
    [view addSubview:self.endBtn];
     self.endBtn.frame=CGRectMake(APPW/2+kmargin*2,kmargin,kmargin*4,kmargin*4);

    //    if ([self.taskModel.status isEqualToString:@"5"])
    //    {
    //        self.endBtn.hidden=YES;
    //    }
    //    else
    //    {
    //        self.endBtn.frame=CGRectMake(APPW/2+kmargin*2,kmargin,kmargin*4,kmargin*4);
    //        self.endBtn.hidden=NO;
    //    }


//    if ([self.taskModel.status isEqualToString:@"5"])
//    {
//        self.endBtn.hidden=YES;
//    }
//    else
//    {
//        self.endBtn.frame=CGRectMake(APPW/2+kmargin*2,kmargin,kmargin*4,kmargin*4);
//        self.endBtn.hidden=NO;
//    }


    // 暂停
    _stopBtn=[AppUI createButtonWithSupeView:self.view andWithNormalImage:[UIImage imageNamed:@"playIcon"] andWithSelectImage:[UIImage imageNamed:@"stopIcon"] andWithBtnAction:^{

        [self startProgressBtn:_stopBtn];

    }];
    [view addSubview:_stopBtn];
    _stopBtn.frame=CGRectMake(APPW/2-kmargin*6,kmargin,kmargin*4,kmargin*4);


    UITextView *textView=[[UITextView alloc]initWithFrame:CGRectMake(kmargin*2,64+160,APPW-kmargin*4,APPH/8)];
    textView.delegate=self;
    textView.textColor=DarkGreyColor;
    textView.placeholder=@"请输入日志记录";
    textView.font=Font_Number(16);
//    ViewBorderRadius(textView,kmargin,1,MAIN_BACK_COLOR);
    [self.view addSubview:textView];
    

    // 确认提交
    [AppUI initNavigationItem:self.navigationItem andWithText:@"保  存" andTextColor:WhiteColor andIsRight:YES withBlock:^{

        if (textView.text.length==0)
        {
            [self.view makeToast:@"请输入日志记录" duration:.5 position:CSToastPositionCenter];
        }
        else
        {
            NSString *taskProgress;
            if (_isEndProgress==YES)
            {
              taskProgress=@"-1";
            }
            else
            {
                taskProgress=[NSString stringWithFormat:@"%.f",self.progressPopUpView.progress*100];
            }
            [API UpdateTaskStatusWithTaskID:self.taskModel.taskID tempo:taskProgress log:textView.text attachments:@"" success:^(id res) {
                NSLog(@"sdhgshdgsdfh9----%@",res[@"error"]);

                if ([res[@"rt"] isEqual:@(1)])
                {
                    [self.view makeToast:@"日志更新成功" duration:1 position:CSToastPositionCenter];

                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.0*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                        [self.navigationController popViewControllerAnimated:NO];
                    });
                }
                else
                {
                     [self.view makeToast:res[@"error"] duration:1 position:CSToastPositionCenter];
                }

            } failure:^(NSInteger stateCode) {

            }];
        }
    }];
}

-(void)startProgressBtn:(UIButton *)sender
{
    sender.selected=!sender.selected;

    [self progress];

}

-(void)progress
{
    _progressPopUpView.popUpViewAnimatedColors=@[MainColor,MainColor];

    if (self.progressPopUpView.progress>=1.0)
    {
        self.stopBtn.selected=NO;
        self.stopBtn.enabled=NO;
    }

    float progress=self.progressPopUpView.progress;
    if (self.stopBtn.selected&&progress<1.0)
    {
        progress+=self.stopBtn.selected?0.005:0.1;
        [self.progressPopUpView setProgress:progress animated:YES];
        [NSTimer scheduledTimerWithTimeInterval:self.stopBtn.selected?0.05:0.5
                                         target:self
                                       selector:@selector(progress)
                                       userInfo:nil
                                        repeats:NO];
    }
}

// 点击return键时候键盘下去
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
